{include file='common/head'}
<body>
<div class="x-body">
    <form class="layui-form" method="post" action="" enctype="multipart/form-data">


        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
             	图片
            </label>
            <div class="layui-upload">
                <button type="button" class="layui-btn "  id="cover">上传图片</button>
                
            </div>
        </div>
        <div class="layui-input-inline" id="preview" name="img2">
           
            <input type="hidden" id="picture" name="picture" value="" >
            
        </div>
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                用户ID
            </label>
            <div class="layui-input-inline">
               <input type="text" id="userid" name="uid" required="" lay-verify="nikename"
                       autocomplete="off" class="layui-input" placeholder="请填写用户id">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>群组
            </label>
            <div class="layui-input-inline">
                <select name="gid" id="jname"  style="width:96px; " lay-verify="required">
                    {volist name="group" id="rs"}
                    <option value=""></option>
                    <option value="{$rs.id}">{$rs.group_name}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
              标题
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="title" required="" lay-verify="nikename"
                       autocomplete="off" class="layui-input" placeholder="供应名称，规格等关键信息">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">标题不能超过28字</span>
            </div>
        </div>

       <!-- <div class="layui-form-item layui-form-text">
            <label for="desc" class="layui-form-label">
              <span class="x-red">*</span>
                地址
            </label>
            <div class="layui-input-block">
                <input type="text"  lay-verify="required" name="address" autocomplete="off" class="layui-input">
            </div>
        </div> -->
        <div class="layui-form-item layui-form-text">
            <label for="desc" class="layui-form-label">
                内容
            </label>
            <div class="layui-input-block">
               
                 <textarea  name="content" class="layui-textarea" lay-verify="required"  placeholder="详细介绍你的产品，包括质量特色和一些服务"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit="">
                发布
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            nikename: function(value){
                if(value.length > 28){
                    return '标题不能超过28个字符！';
                }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#L_pass').val()!=$('#L_repass').val()){
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            console.log(data);
            var url="";
            $.ajax({
                type: "post",
                async:false,
                url: "{:url('index/xinxi/wenzhangadd')}",
                data: data.field,
                success:function (res) {
                    if(res.code==1){
                        layer.msg(res.msg);
                        setTimeout(function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            window.parent.location.reload();//刷新父级窗口
                        }, 2000);
                    }
                    else
                    {
                        layer.msg(res.msg);
                    }
                }
            })
            //发异步，把数据提交给php
            //layer.alert("增加成功", {icon: 6},function () {
            // 获得frame索引
            //var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            // parent.layer.close(index);
            //  });
            return false;
        });


    });
</script>
<script>
    layui.use('upload', function() {
        var upload = layui.upload;
        var $ = layui.jquery;
        var uploadInst = upload.render({
            elem:'#cover'
            ,url:'{:url("index/xinxi/doUpload")}'
            ,multiple: 'ture'
            ,accept:'images'  // 允许上传的文件类型
            ,auto:true // 自动上传
            ,before:function (obj) {
                //console.log(obj);
                // 预览
                obj.preview(function(index,file,result) {
                    // console.log(file.name);    //图片名字
                    // console.log(file.type);    //图片格式
                    // console.log(file.size);    //图片大小
                    // console.log(result);       //图片地址
                    $('#preview').append('<img src="' + result + '" alt="' + file.name +
                        '" class="layui-upload-img" style="width: 40%;height: 40%;objec-fit: cover;;margin-left:10px">')
                    // $('#preview').attr('src',result); //图片链接 base64

                });
                // layer.load();
            }
            // 上传成功回调
            ,done:function(res) {
                // console.log(upload);
                // console.log(res);
                if (res.code == 1) {
                    $('#picture').val($('#picture').val()+res.url+',');//alert( $('#picture').val());
                    return layer.msg(res.msg);
                }
                return layer.msg(res.msg);
            }
            // 上传失败回调
            ,error:function(index,upload) {
                return layer.msg(res.msg);
            }
        });
    });
</script>

</body>

</html>